import dayjs from 'dayjs'
import { request } from '../../request'
import { uuid } from '@/utils'

export const defaultArticle: ApiModels.base.Article = {
  title: '',
  categoryMaps: [],
  agree: 0,
  content: '',
  creationDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  disAgree: 0,
  hits: 0,
  id: uuid.new(),
  editor: '',
  img: '',
  tagMaps: [],
}

export type ArticleQueryParamsType = ApiModels.IBaseQueryParams & {
  categorynum?: string
  startdate?: string
  enddate?: string
  overdate?: string
  top?: number
}

/**
 * 获取资讯分页列表
 * @param params - 查询参数
 */
export function fetchArticleList(params?: ArticleQueryParamsType) {
  return request.get<ApiModels.PageList<ApiModels.base.Article>>('/base/articles/list', { params })
}

/**
 * 获取资讯详情
 * @param id - 资讯id
 */
export function fetchArticleDetail(id: string) {
  return request.get<ApiModels.base.Article>(`/base/articles/get/${id}`)
}

/**
 * 新增资讯
 * @param data - 资讯模型
 */
export function createArticle(data: ApiModels.base.Article) {
  return request.post<ApiModels.base.Article>('/base/articles/create', data)
}

/**
 * 更新资讯
 * @param data - 资讯模型
 */
export function updateArticle(data: ApiModels.base.Article) {
  return request.put<ApiModels.base.Article>('/base/articles/update', data)
}

/**
 * 删除资讯
 * @param id - 资讯id
 */
export function deleteArticle(id: string) {
  return request.delete<ApiModels.base.Article>(`/base/articles/delete/${id}`)
}

/**
 * 批量删除资讯
 * @param idArray - 资讯id数组
 */
export function batchDeleteArticle(idArray: string[]) {
  return request.delete<ApiModels.base.Article>('/base/articles/batchDelete', { data: idArray })
}
